<!-- src/views/stocktaking/ConfirmStocktaking.vue -->
<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 接收传入的行数据
const props = defineProps({
  rowData: {
    type: Object,
    required: true
  }
})

// 盘点数量输入值
const correctCount = ref(null)

// 表单引用
const formRef = ref()

// 验证规则
const rules = {
  correctCount: [
    { required: true, message: '请输入盘点数量', trigger: 'blur' },
    { type: 'number', min: 0, message: '盘点数量不能小于0', trigger: 'blur' }
  ]
}

// 提交确认
const handleSubmit = async (callback) => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      // 计算差异数量
      const discrepancyCount = correctCount.value - props.rowData.bookCount
      
      // 构造提交数据
      const submitData = {
        id: props.rowData.id,
        correctCount: correctCount.value,
        discrepancyCount: discrepancyCount,
        status: 2 // 确认为已确认状态
      }
      
      // 调用回调函数传递数据
      callback(submitData)
    } else {
      ElMessage.warning('请正确填写盘点数量')
    }
  })
}

// 暴露方法给父组件
defineExpose({
  handleSubmit
})

onMounted(() => {
  // 初始化时设置焦点
  setTimeout(() => {
    const input = document.querySelector('.correct-count-input input')
    if (input) {
      input.focus()
    }
  }, 100)
})
</script>

<template>
  <div class="confirm-stocktaking-container">
    <el-form 
      ref="formRef" 
      :model="{ correctCount }" 
      :rules="rules" 
      label-width="120px"
      @submit.prevent
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="商品名称">
            <el-input v-model="rowData.title" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库名称">
            <el-input v-model="rowData.targetWarehouseName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="库区名称">
            <el-input v-model="rowData.targetWarehouseAreaName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库位名称">
            <el-input v-model="rowData.targetWarehouseLocaName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账面数量">
            <el-input v-model="rowData.bookCount" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="盘点数量" prop="correctCount">
            <el-input-number
              v-model="correctCount"
              class="correct-count-input"
              controls-position="right"
              :min="0"
              placeholder="请输入盘点数量"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
.confirm-stocktaking-container {
  padding: 20px;
}

:deep(.el-input.is-disabled .el-input__inner) {
  background-color: #f5f7fa;
  color: #606266;
}

.correct-count-input {
  width: 100%;
}
</style>